package com.chenxinzhimusic.page

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.chenxinzhimusic.api.Api
import com.chenxinzhimusic.viewmodel.MusicSearchViewModel
import kotlinx.coroutines.launch

@Composable
fun SearchSuggestionList(
    viewModel: MusicSearchViewModel,
    onClick: () -> Unit = {},
    modifier: Modifier = Modifier
) {
    val rememberCoroutineScope = rememberCoroutineScope()
    LazyColumn(modifier = modifier.fillMaxWidth()) {
        items(viewModel.searchTipList) { item ->
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .clickable {
                        rememberCoroutineScope.launch {
                            onClick()
                            viewModel.searchContent = item.text
                            viewModel.searchContentAction = item.text
                        }
                    }
                    .padding(horizontal = 16.dp, vertical = 12.dp)
                    ,
                verticalAlignment = Alignment.CenterVertically
            ) {
                Icon(
                    imageVector = Icons.Default.Search,
                    contentDescription = null,
                    tint = Color.Gray,
                    modifier = Modifier.size(20.dp)
                )

                Spacer(modifier = Modifier.width(12.dp))
                Text(
                    text = item.text,
                    color = Color.White,
                    modifier = Modifier.weight(1f),
                    maxLines = 1,
                    overflow = TextOverflow.Ellipsis
                )

            }
            HorizontalDivider(
                modifier = Modifier.padding(horizontal = 16.dp),
                thickness = 0.5.dp,
                color = Color.Gray.copy(alpha = 0.2f)
            )
        }
    }
}
